package com.tour.controller;

import com.tour.bean.ReturnT;
import com.tour.bean.UserMessage;
import com.tour.service.ManageService;
import com.tour.service.UserMessageService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName ManageController
 * @Author 张梦兴
 * @Date 2020/3/13 23:52
 * @describe
 * 后台管理
 */
@Controller
public class ManageController {

    private final Logger log= Logger.getLogger(ManageController.class);
    @Autowired
    private ManageService service;

    @Autowired
    private UserMessageService userMessageService;

    /**
     * 接受登录页提交的信息，并保持
     * @param loginName
     * @param loginPassword
     * @param request
     * @return
     */
    @RequestMapping("/manage/adminLogin")
    public String toManage(String loginName, String loginPassword, HttpServletRequest request, Map<String,Object> map){
        ReturnT<String> returnT = service.vaildLogin(loginName, loginPassword);
        if(returnT.getCode()==200){
            request.getSession().setAttribute("admin", "admin");
            return "redirect:/managePage";
        }
        map.put("code", 500);
        return "forward:/manage";
    }

    @RequestMapping("/admin/replyMsg")
    @ResponseBody
    public ReturnT<String> replyMsg(@RequestBody Map<String,String> map){
        if (map.containsKey("msgId")&&map.containsKey("message")) {
            String message=map.get("message");
            Integer msgid=Integer.parseInt(map.get("msgId"));
            if (StringUtils.isEmpty(message)||StringUtils.isEmpty(msgid)){
                return new ReturnT<>(402, "缺失必要参数");
            }
            UserMessage msg=new UserMessage();
            msg.setId(msgid);
            msg.setIsReply("1");
            msg.setReplyMsg(message);
            userMessageService.update(msg);
        }else {
            return new ReturnT<>(402, "缺失必要参数");
        }
        return ReturnT.SUCCESS;
    }
}
